package com.ketangpai.admin.utils.config;

import org.jetbrains.annotations.NotNull;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//创建一个自定义的日期转换器类，实现Converter<String, Date>接口
public class StringToDateConverter implements Converter<String, Date> {
    private final SimpleDateFormat dateFormat;

    public StringToDateConverter(String pattern) {
        this.dateFormat = new SimpleDateFormat(pattern);
    }

    @Override
    public Date convert(@NotNull String source) {
        try {
            return dateFormat.parse(source);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Invalid date format");
        }
    }
}